package demo1.web.servlet.userMaiapi;

import demo1.dao.Mapper.goodsMapper;
import demo1.pojo.userMai;
import demo1.dao.sql.goodssql;
import demo1.service.userMaiService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/updateGoods")
public class updateGoods extends HttpServlet {//修改商品的信息
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        //获取更新的数据
        String goodsName = req.getParameter("goodsName");
        String goodsMessage = req.getParameter("goodsMessage");
        int price = Integer.parseInt(req.getParameter("price"));
        int goodsID = Integer.parseInt(req.getParameter("goodsID"));
        String goodsType = req.getParameter("typeSelection");

        //获取session对象
        HttpSession session = req.getSession();
        Object userObject = session.getAttribute("users");
        userMai users = (userMai) userObject;

        try {
            userMaiService update = new userMaiService();
            update.updateGoodsName(goodsName,users.getId(),goodsID);//更新名称
            update.updateGoodsMessage(goodsMessage,users.getId(),goodsID);//更新商品信息
            update.updateGoodsType(goodsType,users.getId(),goodsID);//更新商品种类
            update.updateGoodsprice(price,users.getId(),goodsID);//更新商品价格
            resp.sendRedirect("/logindemo2/showgoods");//重定向到展示商品的界面
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
